/**
 * OtherProducts
 * @Chunning.Xu
 * 2011 Nov 24, 2011 11:05:43 AM
 **/
package
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Cubic;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.utils.ByteArray;
	import flash.utils.setTimeout;
	
	[SWF(width=596,height=555,backgroundColor=0x000000)]
	public class OtherProducts extends Sprite
	{
		//START.
		
		public static function convertByteToXML(byteClass:Class):XML
		{
			var bytes:ByteArray = new byteClass;
			var strData:String = bytes.readUTFBytes(bytes.length);
			
			XML.ignoreWhitespace = true;
			var xml:XML = new XML(strData);
			
			return xml;
		}
		
		[Embed(source="xml/other_products.xml",mimeType="application/octet-stream")]
		private var productsClass:Class;
		
		private var view:Sprite;
		
		private var btnPurchase1:Sprite;
		private var btnPurchase2:Sprite;
		private var btnPurchase3:Sprite;
		
		private var tip:String;
		private var share:String;
		private var purchases:XMLList;
		private var trackings:XMLList;
		private var products:Array;
		
		public function OtherProducts()
		{
			view = new OtherProductViewUI;
			
			var xml:XML = convertByteToXML(productsClass);
			share = xml["share"].toString();
			tip = xml["tip"].toString();
			
			purchases = xml["purchase"];
			trackings = xml["tracking"];

			try{
				var url:String = ExternalInterface.call("window.location.href.toString");
				ExternalInterface.call("showTip",tip,share,url);	
			}catch(err:Error){trace(err.message)}
			
			btnPurchase1 = view.getChildByName("btnPurchase1") as Sprite;
			btnPurchase1.buttonMode = true;
			btnPurchase2 = view.getChildByName("btnPurchase2") as Sprite;
			btnPurchase2.buttonMode = true;
			btnPurchase3 = view.getChildByName("btnPurchase3") as Sprite;
			btnPurchase3.buttonMode = true;
			
			btnPurchase1.addEventListener(MouseEvent.CLICK,onPurchaseButtonClicked);
			btnPurchase2.addEventListener(MouseEvent.CLICK,onPurchaseButtonClicked);
			btnPurchase3.addEventListener(MouseEvent.CLICK,onPurchaseButtonClicked);
			
			products = [btnPurchase1,btnPurchase2,btnPurchase3];
			
			view.alpha = 0;
			this.addChild(view);
			
			TweenLite.to(view,1.0,{ease:Cubic.easeInOut,alpha:1.0});
		}
		
		private function trackClick(target:Object):void
		{
			if(target == btnPurchase1)
			{	
				if(ExternalInterface.available)
					ExternalInterface.call("_gaq.push",["_trackEvent",trackings[0].toString(),"click"]);
				
			}else if(target == btnPurchase2){
				if(ExternalInterface.available)
					ExternalInterface.call("_gaq.push",["_trackEvent",trackings[1].toString(),"click"]);
				
			}else if(target == btnPurchase3){
				if(ExternalInterface.available)
					ExternalInterface.call("_gaq.push",["_trackEvent",trackings[2].toString(),"click"]);
			}
		}
		
		private function onPurchaseButtonClicked(evt:Event):void
		{
			navigateToURL(new URLRequest(purchases[products.indexOf(evt.currentTarget)].toString()),"_blank");
			setTimeout(trackClick,100,evt.currentTarget);
		}
		
		//END
	}
}